﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_PositionCommands : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IPositionCommandBLL CmdPos = new PositionCommandBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();

        private EmployeeInfo empInfo = new EmployeeInfo();
        Main main = new Main();
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            FillDataToRecordStatus();
            FillDataToApprover();

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {

                    cmbCmdStatus.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "CmdStatusSeq"));
                    txtRefDate.SetValue(DataBinder.Eval(DataItem, "RefDate", "{0:dd/MM/yyyy}").ToString());
                    txtApprovedDate.SetValue(DataBinder.Eval(DataItem, "ApprovedDate", "{0:dd/MM/yyyy}").ToString());
                    if (DataBinder.Eval(DataItem, "ApprovedBy") == null)
                    {
                        txtApprovedName.Text = DataBinder.Eval(DataItem, "ApprovedName").ToString();
                    }
                    else
                    {
                        cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "ApprovedBy").ToString();
                        txtApprovedName.Text = "";
                    }

                }
                else
                {

                }
                rdoInternal.Checked = (cmbEmployee.SelectedValue.ToString() != string.Empty && txtApprovedName.Text.Trim().Length <= 0);
                rdoExternal.Checked = !rdoInternal.Checked;
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        public void FillDataToApprover()
        {
            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];
            //cmbEmployee.DataSource = dt;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }
        public void FillDataToRecordStatus()
        {
            IMasRecordStatusBLL mas = new MasRecordStatusBLL();
            IList<MasRecordStatusInfo> infoList = mas.ListStatusCommand();

            cmbCmdStatus.DataTextField = "Name";
            cmbCmdStatus.DataValueField = "Status";
            cmbCmdStatus.DataSource = infoList;
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            PositionCommandInfo Info = new PositionCommandInfo();

            CheckApproveStatus();

            //Info.RefNo = txtRefNo.Text.Length > 0 ? txtRefNo.Text : "";
            Info.RefDate = txtRefDate.GetStringValue().Length > 0 ? DateTime.Parse(txtRefDate.GetStringValue()) : (DateTime?)null;
            Info.CmdRefNo = txtCmdRefNo.Text.Length > 0 ? txtCmdRefNo.Text : "";
            Info.ApprovedDate = txtApprovedDate.GetStringValue().Length > 0 ? DateTime.Parse(txtApprovedDate.GetStringValue()) : (DateTime?)null;
            Info.Description = txtCmdPosDescr.Text.Length > 0 ? txtCmdPosDescr.Text : "";
            Info.CmdStatusSeq = cmbCmdStatus.SelectedValue.Length > 0 ? cmbCmdStatus.SelectedValue : "";
            Info.ApprovedBy = cmbEmployee.SelectedValue.Length > 0 ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null;
            Info.ApprovedName = txtApprovedName.Text.Length > 0 ? txtApprovedName.Text : "";
            Info.ApprovedPosition = txtApprovedPosition.Text.Length > 0 ? txtApprovedPosition.Text : "";
            Info.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            Info.EmployeeTypeSeq = Session["EMPTYPE"].ToString().Length > 0 ? Int32.Parse(Session["EMPTYPE"].ToString()) : (int?)null;
            Info.CreatedBy = ssMng.EmpSeqI;

            CmdPos.Insert(Info);
        }

        protected void CheckApproveStatus()
        {
            if (cmbCmdStatus.SelectedValue == SystemInfo._ApproveRecordStatus && txtApprovedDate.GetStringValue().Length == 0)
            {
                main.MessageAlert(this.Page, "กรุณาระบุคำสั่งลงวันที่!!");
                return;
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            PositionCommandInfo Info = new PositionCommandInfo();

            CheckApproveStatus();

            Info.Seq = lblCmdPosSeq.Text.Length > 0 ? Int32.Parse(lblCmdPosSeq.Text) : (int?)null;
            //Info.RefNo = txtRefNo.Text.Length > 0 ? txtRefNo.Text : "";
            Info.RefDate = txtRefDate.GetStringValue().Length > 0 ? DateTime.Parse(txtRefDate.GetStringValue()) : (DateTime?)null;
            Info.CmdRefNo = txtCmdRefNo.Text.Length > 0 ? txtCmdRefNo.Text : "";
            Info.ApprovedDate = txtApprovedDate.GetStringValue().Length > 0 ? DateTime.Parse(txtApprovedDate.GetStringValue()) : (DateTime?)null;
            Info.Description = txtCmdPosDescr.Text.Length > 0 ? txtCmdPosDescr.Text : "";
            Info.CmdStatusSeq = cmbCmdStatus.SelectedValue.Length > 0 ? cmbCmdStatus.SelectedValue : "";
            Info.ApprovedBy = cmbEmployee.SelectedValue.Length > 0 ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null;
            Info.ApprovedName = txtApprovedName.Text.Length > 0 ? txtApprovedName.Text : "";
            Info.ApprovedPosition = txtApprovedPosition.Text.Length > 0 ? txtApprovedPosition.Text : "";
            Info.Remarks = txtRemarks.Text.Length > 0 ? txtRemarks.Text : "";
            Info.EmployeeTypeSeq = Session["EMPTYPE"].ToString().Length > 0 ? Int32.Parse(Session["EMPTYPE"].ToString()) : (int?)null;
            Info.UpdatedBy = ssMng.EmpSeqI;

            CmdPos.Update(Info);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (rdoInternal.Checked)
            {
                txtApprovedName.Text = "";
                if (cmbEmployee.SelectedValue.Length > 0)
                {
                    IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
                    empInfo = empList[0];
                    txtApprovedPosition.Text = empInfo.Position.Executive.Name;
                }
            }
        }

    }
}